<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Minime_User_Subscriptions {
    
    private $subscriptions = array();
    
    const ELEMENT_SUBSCRIPTIONS = "subscriptions";
    
    public function __construct() {
        
    }

    public function addSubscription(Minime_User_Subscription $sub) {
        $this->subscriptions[$sub->getUri()] = $sub;
    }
    
    /**
     * @param $uri
     * @return Minime_User_Subscription
     */
    public function getSubscriptionByUri($uri) {
        if(isset($this->subscriptions[$uri])) {
            return $this->subscriptions[$uri];
        }
        return null;
    }
    
    public function getSubscriptions() {
        return $this->subscriptions;
    }
    
    public function asXml() {
        
        $doc = new DOMDocument('1.0', 'UTF-8');
        // we want a nice output
        $doc->formatOutput = true;

        $root = $doc->createElement(self::ELEMENT_SUBSCRIPTIONS);
        $root = $doc->appendChild($root);
        
        foreach($this->subscriptions as $subscription) {
            $root->appendChild($subscription->asDOMElement($doc));
        }
        
        return $doc->saveXML();
    }
   
    /**
     * We should have input something similar to this in the method:
     * 
     * <subscriptions>
     *   <subscription>
     *     <username>http://callback.com/callback.php</username>
     *     <atomfeed>http://identi.ca/api/statuses/user_timeline/209172.atom</atomfeed>
     *     <alternate>http://identi.ca/api/statuses/user_timeline/209172.atom</alternate>
     *     <logo>SECRETQSDF345SDRT35ERG35SDFG35SRG</logo>
     *   </subscription>
     *   ...
     * <subscriptions>
     * @return Minime_User_Subscriptions 
     */
    public static function buildFromRawXML($rawXML) {
        $dom = new DOMDocument();
        if(@$dom->loadXML($rawXML)) {
            return self::buildFromDom($dom);
        }
        throw new Exception("Invalid raw XML data provided!");
    }
    
    public static function buildFromDom(DOMDocument $dom) {
        
        $subscriptions = new Minime_User_Subscriptions();
        
        $subscriptions_elm = $dom->getElementsByTagName(self::ELEMENT_SUBSCRIPTIONS)->item(0);
        if(!$subscriptions_elm) {
            throw new Exception("Invalid XML! No subscriptions root element found!");
        }
        
        foreach ($subscriptions_elm->getElementsByTagName('subscription') as $subscriptions_elm) {
            $subscriptions->addSubscription(Minime_User_Subscription::buildFromDOMElement($subscriptions_elm));
        }
        
        return $subscriptions;
    }
}